const Service = require("egg").Service;

class ShareService extends Service {
    async getList() {
        const { ctx, app } = this;
        const {
            user_id
        } = ctx.request.body;
        let list = await app.mysql.select("shares", { user_id });
        list.sort((a, b) => {
            return b.create_time - a.create_time
        })
        return {
            total: list.length,
            data: list
        }
    }
    //删除
    async del() {
        const { ctx, app } = this;
        const { id } = JSON.parse(ctx.query.params);;

        let res = await app.mysql.delete("roomList", { id });
        return res;
    }
    //上传图片
    async upload() {
        const { ctx } = this;
        const {
            name
        } = ctx.request.body;
        let res;
        if (name) {
            try {
                res = await ctx.oss.delete(name)
            } catch (err) {
                console.log(err);
            }
        } else {
            const file = ctx.request.files[0];
            const name = Date.now() + file.fieldname;
            try {
                res = await ctx.oss.put(name, file.filepath);
            } catch (err) {
                console.log(err);
            }
        }


        return res
    }
    //上传楼市圈
    async addShare() {
        const { ctx, app } = this;
        let {
            content,
            name,
            headImg,
            user_id,
            img
        } = ctx.request.body;
        let res = await app.mysql.insert("shares", {
            content,
            name,
            headImg,
            user_id,
            create_time: Date.now(),
            img
        })
        return res
    }
}
module.exports = ShareService;